package asu.util
{
	import flash.display.DisplayObject;
	
	public class DisplayUtil
	{
		public static function getEndPosition( itemPosition:Number, itemWidth:Number, containerWidth:Number ):Number 
		{
			return containerWidth - itemPosition - itemWidth;
		}
		
		public static function getCenterPosition( containerWidth:Number, offset:Number=0 ):Number 
		{
			return (containerWidth - offset)/2;
		}
		
		public static function center( target:Object, parent:Object=null, centerAnchor:Boolean = false, h:Boolean=true, v:Boolean=true ):void
		{
			if( !parent && !target.parent ) return;
			if( !parent ) parent = target.parent;
			//trace( 'Styler::center', parent.width, parent.height );
			
			var anchorX:Number = ( centerAnchor ) ? target.height/2 : 0;
			var anchorY:Number = ( centerAnchor ) ? target.width/2 : 0;
			
			if( h ) target.y = (parent.height-target.height)/2 + anchorY;
			if( v ) target.x = (parent.width-target.width)/2 + anchorX;
		}
		
		public static function align( target:DisplayObject, verticalAlign:String=null, horizontalAlign:String=null, alignTarget:DisplayObject=null ):void
		{
			if( !alignTarget ) alignTarget = target.parent;
			if( !alignTarget ) return;
			var isAlignTargetParent:Boolean = ( alignTarget == target.parent );
			var pos:Number;
			if( verticalAlign )
			{
				switch( verticalAlign )
				{
					case 'top':
						target.y = isAlignTargetParent ? 0 : alignTarget.y;
					break;
					
					case 'center':
						pos = ( alignTarget.height - target.height )/2;
						target.y = isAlignTargetParent ? pos : pos + alignTarget.y;
					break;
					
					case 'bottom':
						pos = ( alignTarget.height - target.height );
						target.y = isAlignTargetParent ? pos : pos + alignTarget.y;
					break;
				}
			}
			
			if( horizontalAlign )
			{
				switch( horizontalAlign )
				{
					case 'left':
						target.x = isAlignTargetParent ? 0 : alignTarget.x;
					break;
					
					case 'center':
						pos = ( alignTarget.width - target.width )/2;
						target.x = isAlignTargetParent ? pos : pos + alignTarget.x;
					break;
					
					case 'right':
						pos = ( alignTarget.width - target.width );
						target.x = isAlignTargetParent ? pos : pos + alignTarget.x;
					break;
				}
			}
		}
	}
}